package com.gcsj.gcsjspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gcsj.gcsjspringboot.common.utils.PageUtils;
import com.gcsj.gcsjspringboot.common.utils.R;
import com.gcsj.gcsjspringboot.entity.Incentive;
import com.gcsj.gcsjspringboot.service.IncentiveService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/incentive")
public class IncentiveController extends AbstractController{
    @Autowired
    IncentiveService incentiveService;

    @PostMapping("/insert")
    @RequiresRoles("admin")
    public R insert(@RequestBody Incentive incentive){
        boolean ret;
        if(incentive.getId() != null){
            ret = incentiveService.updateById(incentive);
        } else {
            ret = incentiveService.save(incentive);
        }
        if(ret){
            return R.ok();
        }
        return R.error();
    }

    @PostMapping("/update")
    @RequiresRoles("admin")
    public R update(@RequestBody Incentive incentive){
        boolean ret = incentiveService.updateById(incentive);
        if(ret){
            return R.ok();
        }

        return R.error();
    }
    @PostMapping("/del")
    @RequiresRoles("admin")
    public R del(@RequestBody Incentive incentive){
        boolean ret = incentiveService.removeById(incentive.getId());
        if(ret){
            return R.ok();
        }
        return R.error();
    }

    @PostMapping("/page")
    public R query(@RequestBody Map<String, Object> params){
        Long userId = getUserId();
        params.put("userId", userId);
        PageUtils page = incentiveService.queryPage(params);
        return R.ok().put("page", page);
    }

}
